/*
 * @Author: your name
 * @Date: 2020-08-25 00:46:59
 * @LastEditTime: 2020-08-25 21:19:48
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \新建文件夹\two-app\src\store\addUser\index.js
 */
import { addEdit, setIdentityView, addUser, putUser, getUser, addAuthorityApi, getViewAuthority, addViewAuthorityView, addApiAuthority, getIdentity, setIdentityApi } from '../../api/addUser'
import { Message } from 'view-design'

const state = {
  authority: [], // 身份id
  viewAuthority: [], // 获取视图权限数据
  apiAuthority: [], // api接口权限数据
  userData: []// 用户数据
}

const mutations = {
  // 获取身份数据
  getUserIdentity(state, action) {
    state.authority = action
  },
  // 获取视图权限数据
  getViewAuthority(state, action) {
    state.viewAuthority = action
  },
  // api接口权限数据
  getApiAuthority(state, action) {
    state.apiAuthority = action
  },
  // 用户数据
  getUser(state, action) {
    state.userData = action
  }
}

const actions = {
  // 添加用户
  async adduser({ commit }, action) {
    const data = await addUser(action)
    if (data.data.code !== 1) {
      return Message.error(data.data.err.errors[0].message)
    }
    Message.success(data.data.msg)
  },
  // 获取用户数据
  async getuser({ commit }) {
    const data = await getUser()
    commit('getUser', data.data.data)
  },
  // 更新用户信息
  async putuser({ commit }, action) {
    const data = await putUser(action)
    console.log(data, '----更新用户')
    if (data.data.code !== 1) {
      return Message.error(data.data.msg)
    }
    Message.success(data.data.msg)
  },
  // 获取身份数据
  async getuseridentity({ commit }) {
    const data = await getIdentity()
    // console.log(data, '----身份数据')
    commit('getUserIdentity', data.data.data)
  },
  // // 身份id
  // async getauthority({ commit }) {
  //   const data = await getAuthority()
  //   // console.log(data.data.data)
  //   commit('getAuthority', data.data.data)
  // },
  // 添加身份
  async addEdit({ commit }, action) {
    const data = await addEdit(action)
    console.log(data)
    if (data.data.code !== 1) {
      return Message.error(data.data.msg)
    }
    Message.success(data.data.msg)
  },
  // 添加api接口权限
  async addAuthorityApi({ commit }, action) {
    const data = await addAuthorityApi(action)
    if (data.data.code !== 1) {
      return Message.error(data.data.msg)
    }
    Message.success(data.data.msg)
  },
  // 获取视图权限数据
  async getviewauthority({ commit }) {
    const data = await getViewAuthority()
    // console.log(data.data.data, '---视图权限')
    commit('getViewAuthority', data.data.data)
  },
  // 添加视图权限
  async addviewauthorityview({ commit }, action) {
    const data = await addViewAuthorityView(action)
    if (data.data.code !== 1) {
      return Message.error(data.data.msg)
    }
    Message.success(data.data.msg)
  },
  // 获取api接口权限数据
  async getapiauthority({ commit }) {
    const data = await addApiAuthority()
    // console.log(data, 'api接口权限')
    commit('getApiAuthority', data.data.data)
  },
  // 给身份设置api接口权限
  async setidentityapi({ commit }, action) {
    const data = await setIdentityApi(action)
    // console.log(data, '------给身份设置api权限')
    if (data.data.code !== 1) {
      return Message.error(data.data.msg)
    }
    Message.success(data.data.msg)
  },
  // 给身份设置视图权限
  async setidentityview({ commit }, action) {
    const data = await setIdentityView(action)
    if (data.data.code !== 1) {
      return Message.error(data.data.msg)
    }
    Message.success(data.data.msg)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
